package com.design.twords.controller;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Create by Alen
 * Time:  17:01
 */
public class TimeController {

    private static final String DTAE_FORMAT = "yyyy-MM-dd";

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DTAE_FORMAT);

    private static final String TAG = "TimeController";

    //获取当前时间
    public static long getCurrentDate(){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        long time = 0;
        try {
            time = simpleDateFormat.parse(year + "-" + month + "-" + day).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //Log.i(TAG, "getCurrentDate:" + time);
        return time;


    }

    // 根据指定日期戳解析成日期形式（yyyy-MM-dd）
    public static String getStringDate(long timeStamp) {
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
    }

    // 根据指定日期戳解析成日期形式（yyyy-MM-dd HH:mm:ss）
    public static String getStringDateDetail(long timeStamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
    }

    // 根据指定日期戳解析成日期形式（yyyy-MM-dd HH:mm）
    public static String getStringDateDetailNoS(long timeStamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
    }

    // 判断两个时间戳是否为同一天
    public static boolean isTheSameDay(long time1, long time2) {
        Log.d(TAG,getStringDate(time1));
        Log.d(TAG,getStringDate(time2));
        return getStringDate(time1).equals(getStringDate(time2));
    }

    // 返回两个日期之间相隔多少天
    public static int daysInternal(long time1, long time2) {
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(getStringDate(time1));
            Date date2 = simpleDateFormat.parse(getStringDate(time2));
            int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
            return days;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }

    // 得到当前时间戳（有日期与时间）
    public static long getCurrentTimeStamp() {
        return System.currentTimeMillis();
    }


}
